<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Parter;

class ParterController extends Controller
{
    
    public function index(Request $request)
    {
        $param = [];
        $query = Parter::query();
        if($request->keyword) {
            $query->where('name','like','%'.$request->keyword.'%');          
            $param['keyword'] = trim($request->keyword);

        }
        $list  = $query->paginate(10);
        return view('admin.parter.index',compact('list','param'));
    }

    
    public function create()
    {
        //
    }

    
    public function store(Request $request)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $data = $request->except(['_token']);      
        if ($request->file('logo')) {
            $code = str_random(20);
            $extension = $request->logo->extension();
            $filename  = $code.'.'.$extension;
            $request->logo->storeAs('public/uploads',$filename);
            $data['logo']   = '/storage/uploads/'.$filename;                
                       
        }
        $flag = Parter::create($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/parters')->with('rs',$rs);
        }
        return back()->withInput()->with('rs',$rs);
    }

   
    public function show($id)
    {
        //
    }

    
    public function edit($id)
    {
        $data =  Parter::find($id);
        return view('admin.parter.edit',compact('data'));
    }

    
    public function update(Request $request, $id)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';
        $data = $request->except(['_token','_method']);      
        if ($request->file('logo')) {
            $code = str_random(20);
            $extension = $request->logo->extension();
            $filename  = $code.'.'.$extension;
            $request->logo->storeAs('public/uploads',$filename);
            $data['logo']   = '/storage/uploads/'.$filename;                
                       
        }
        $flag = Parter::where(['id'=>$id])->update($data);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/parters')->with('rs',$rs);
        }
        return back()->withInput()->with('rs',$rs);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs['status'] = 'danger';
        $rs['msg'] = '操作失败';              
       
        $flag = Parter::destroy($id);
        if($flag) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        }
        return back()->with('rs',$rs);
    }
}
